<?php
namespace app\admin\controller;
// require './vendor/rmccue/requests/library/Requests.php';
// Requests::register_autoloader();
class DeiLi extends AdminBase
{
 
  /**
   * 应用ID
   *
   * @var string
   */
  protected $appKey;

  /**
   * 应用秘钥
   *
   * @var string
   */
  protected $appSecret;

  /**
   * 初始化应用数据
   *
   */
  // public function __construct($config) {
  //   $this->appKey=$config['appKey'];
  //   $this->appSecret=$config['appSecret'];
  // }

  public function index(){
    return 111;
  }
  public function getAppKey() {
    return $this->appKey;
  }

  public function getAppSecret() {
    return 11;
    return $this->appSecret;
  }

  /**
   * 域名
   *
   */
  public $domain = "https://v2-api.delicloud.com";

  // 创建根部门接口
  public $departmentInitPath = "/v2.0/department/init";
  // 查询部门接口
  public $departmentQueryPath = "/v2.0/department/query";
  // 更新部门数据接口
  public $departmentUpdatePath = "/v2.0/department";
  // 为部门设置外部ID接口
  public $departmentSetExtPath = "/v2.0/department/ext";
  // 删除部门
  public $departmentDeletePath = "/v2.0/department/delete";
  // 查询员工接口
  public $employeeQueryPath = "/v2.0/employee/query";
  // 更新员工数据接口
  public $employeeUpdatePath = "/v2.0/employee";
  // 为员工设置外部ID接口
  public $employeeSetExtPath = "/v2.0/employee/ext";
  // 删除员工
  public $employeeDeletePath = "/v2.0/employee/delete";
  // 人员特征更新
  public $cloudappUpdatePath = "/v2.0/cloudappapi";
  // 查询考勤数据命令
  protected $checkinQueryCmd = "/v2.0/cloudappapi";

  /**
   * 创建根部门
   */
  public function departmentInit($headers, $params) {
    $this->makeRequest($this->departmentInitPath, $headers, json_encode($params));
  }

  /**
   * 分页查询部门数据
   */
  public function departmentQuery($headers, $params) {
    $this->makeRequest($this->departmentQueryPath, $headers, json_encode($params));
  }

  /**
   * 更新部门数据
   */
  public function departmentUpdate($headers, $params) {
    $this->makeRequest($this->departmentUpdatePath, $headers, json_encode($params));
  }

  /**
   * 为部门设置外部ID
   */
  public function departmentSetExt($headers, $params) {
    $this->makeRequest($this->departmentSetExtPath, $headers, json_encode($params));
  }

  /**
   * 删除部门
   */
  public function departmentDelete($headers, $params) {
    $this->makeRequest($this->departmentDeletePath, $headers, json_encode($params));
  }

  /**
   * 分页查询部门成员数据
   */
  public function employeeQuery($headers, $params) {
    $this->makeRequest($this->employeeQueryPath, $headers, json_encode($params));
  }

  /**
   * 更新成员数据
   */
  public function employeeUpdate($headers, $params) {
    $this->makeRequest($this->employeeUpdatePath, $headers, json_encode($params));
  }

  /**
   * 为员工设置外部ID
   */
  public function employeeSetExt($headers, $params) {
    $this->makeRequest($this->employeeSetExtPath, $headers, json_encode($params));
  }

  /**
   * 删除员工
   */
  public function employeeDelete($headers, $params) {
    $this->makeRequest($this->employeeDeletePath, $headers, json_encode($params));
  }

  /**
   * 人员特征更新
   */
  public function cloudappUpdate($headers, $params) {
    $this->makeRequest($this->cloudappUpdatePath, $headers, json_encode($params));
  }

  /**
   * 查询打卡数据
   */
  public function checkinQuery($headers, $params) {
    $this->makeRequest($this->checkinQueryCmd, $headers, json_encode($params));
  }

  /**
   * 使用给定的数据进行签名
   *
   * @param array $data
   *
   */
  public function makeSignedRequest($data) {
    if (!is_array($data)) {
      throw new InvalidArgumentException(
        '请输入数组：' . print_r($data, true));
    }
    //将path timestamp key secret依次拼接成字符串
    $sig = $data['path'] . $data['time'] . $data['key'] . $data['secret'];
    //通过MD5加密函数加密
    $result = md5($sig);
    return $result;
  }

  /**
   * 获取13位时间戳
   *
   */
  public function getUnixTimestamp() {
    list($s1, $s2) = explode(' ', microtime());
    return (float)sprintf('%.0f',(floatval($s1) + floatval($s2)) * 1000);
  }

  /**
   * 设置请求信息
   *
   * @param string $path 
   * @param array $headers 
   * @param array $params 
   *
   */
  public function makeRequest($path, $headers, $params) {
    // 获取请求数据
    $appId = $this -> getAppKey();
    $appSecret = $this -> getAppSecret();
    $time = $this -> getUnixTimestamp();
    $domain = $this ->domain;
    // 设置请求头
    $sig = $this->makeSignedRequest(array(
      "path" => $path,
      "time" => $time,
      "key" => $appId,
      "secret" => $appSecret
    ));
    $headers_base = array(
      "Content-type" => "application/json",
      "App-Key" => $appId,
      "App-Timestamp" => $time,
      "App-Sig" => $sig
    );
    if(isset($headers)){
      $headers_base = array_merge($headers, $headers_base);
    }
    $request = Requests::post($domain.$path, $headers_base, $params);
    print($request->body);
    $result = $request->body;
    return $result;
  }
}